#!/bin/bash

# 容器名称
CONTAINER_NAME="DemoChat"

# 本地路径和容器内路径
LOCAL_PATH="/Users/jackiezheng/liushi/Ai_Farmer_Doctor"
CONTAINER_PATH="/app"

# 检查容器是否已经存在
if [[ $(docker ps -a --filter "name=${CONTAINER_NAME}" --format '{{.Names}}') == "$CONTAINER_NAME" ]]; then
  echo "Starting existing Docker container ${CONTAINER_NAME}..."
  docker start $CONTAINER_NAME
else
  echo "Creating and starting new Docker container ${CONTAINER_NAME}..."
  docker run -d --name $CONTAINER_NAME -p 8000:8000 -p 7474:7474 -p 8080:8080 -p 8084:8084 \
    -v $LOCAL_PATH:$CONTAINER_PATH \
    your-docker-image-name:latest /bin/bash -c "sleep infinity"
fi

# 等待 5 秒
echo "Waiting for 5 seconds for the container to initialize..."
sleep 5

# 启动 Django 服务
echo "Starting Django service on port 8000..."
docker exec $CONTAINER_NAME /bin/bash -c "cd /app/Inquired && python3 manage.py runserver 0.0.0.0:8000" &

# 启动 Neo4j 服务
echo "Starting Neo4j service on port 7474..."
docker exec $CONTAINER_NAME /bin/bash -c "cd /app/neo4j-community-4.4.12/bin && ./neo4j console" &

# 启动第一个 Gunicorn 服务，绑定到 8084 端口
echo "Starting Unicorn service on port 8084..."
docker exec $CONTAINER_NAME /bin/bash -c "cd /app/networking && gunicorn -w 1 -b 0.0.0.0:8084 deploy_model:app" &

# 启动第二个 Gunicorn 服务，绑定到 8080 端口
echo "Starting Principal service on port 8080..."
docker exec $CONTAINER_NAME /bin/bash -c "cd /app/networking && gunicorn -w 1 -b 0.0.0.0:8080 principal:app" &

echo "All services started."
